
using System;
using System.Data;
using System.Linq;
using System.Linq.Dynamic;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;

using Abp.UI;
using Abp.AutoMapper;
using Abp.Extensions;
using Abp.Authorization;
using Abp.Domain.Repositories;
using Abp.Application.Services.Dto;
using Abp.Linq.Extensions;

using JYJ.AiYard.Extension;
using JYJ.AiYard.StationManagement.TbYardPayMonthCards;
using JYJ.AiYard.StationManagement.TbYardPayMonthCards.Dtos;
using JYJ.AiYard.StationManagement.TbYardPayMonthCards.DomainService;
using JYJ.AiYard.StationManagement.TbYardPayMonthCards.Authorization;
using JYJ.AiYard.Database;
using JYJ.AiYard.StationManagement.YardReport;
using JYJ.AiYard.StationManagement.YardCommonHald;

namespace JYJ.AiYard.StationManagement.TbYardPayMonthCards
{
    /// <summary>
    /// TbYardPayMonthCard应用层服务的接口实现方法  
    ///</summary>
    [AbpAuthorize]
    public class TbYardPayMonthCardAppService : AiYardAppServiceBase, ITbYardPayMonthCardAppService
    {
        private readonly IRepository<TbYardPayZMonth, long> _entityRepository;

        private readonly ITbYardPayMonthCardManager _entityManager;

        private readonly IAiYardDbTableIssuedCard _aiYardDbTableIssuedCard;

        /// <summary>
        /// 构造函数 
        ///</summary>
        public TbYardPayMonthCardAppService(
        IRepository<TbYardPayZMonth, long> entityRepository
        ,ITbYardPayMonthCardManager entityManager
        , IAiYardDbTableIssuedCard aiYardDbTableIssuedCard
        )
        {
            _entityRepository = entityRepository; 
             _entityManager=entityManager;
             _aiYardDbTableIssuedCard = aiYardDbTableIssuedCard;
        }


        /// <summary>
        /// 获取TbYardPayMonthCard的分页列表信息
        ///</summary>
        /// <param name="input"></param>
        /// <returns></returns>
		[AbpAuthorize(TbYardPayMonthCardPermissions.Query)] 
        public async Task<PagedResultDto<TbYardPayMonthCardListDto>> GetPaged(GetTbYardPayMonthCardsInput input)
		{
			//// TODO:根据传入的参数添加过滤条件 
			List<long> StationList = new List<long>();
			tbQFilterText filterText = input.FilterText.ToObject<tbQFilterText>();
			if (filterText.StationId == null || filterText.StationId <= 0)
			{
				StationList = GetCurrenUserOrganizationAndChildrenUnit();
			}
			else
			{
				StationList.Add((long)filterText.StationId);
			}
			var query1 = _aiYardDbTableIssuedCard.GetTbYardPayMonthCard(StationList, 0, filterText.FilterText);

            var query = query1.AsQueryable();
            var count = query.Count();
             

            var entityList = query
                .OrderBy(input.Sorting).AsNoTracking()
                .PageBy(input)
                .ToList();
             
            var entityListDtos =entityList.MapTo<List<TbYardPayMonthCardListDto>>();

			return new PagedResultDto<TbYardPayMonthCardListDto>(count,entityListDtos);
		}


		/// <summary>
		/// 通过指定id获取TbYardPayMonthCardListDto信息
		/// </summary>
		[AbpAuthorize(TbYardPayMonthCardPermissions.Query)] 
		public async Task<TbYardPayMonthCardListDto> GetById(EntityDto<long> input)
		{
            //var entity = await _entityRepository.GetAsync(input.Id);
            long Id = -1;
            if (input.Id != null && input.Id > 0)
            {
                Id = input.Id;
            }
            var entity = _aiYardDbTableIssuedCard.GetTbYardPayMonthCard(GetCurrenUserOrganizationAndChildrenUnit(), Id, "");

            return entity.MapTo<TbYardPayMonthCardListDto>();
		}

		/// <summary>
		/// 获取编辑 TbYardPayMonthCard
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		[AbpAuthorize(TbYardPayMonthCardPermissions.Create,TbYardPayMonthCardPermissions.Edit)]
		public async Task<GetTbYardPayMonthCardForEditOutput> GetForEdit(NullableIdDto<long> input)
		{
			var output = new GetTbYardPayMonthCardForEditOutput();
            TbYardPayMonthCardEditDto editDto;

			if (input.Id.HasValue)
			{
                //var entity = await _entityRepository.GetAsync(input.Id.Value);
                long Id = -1;
                if (input.Id.Value != null && input.Id.Value > 0)
                {
                    Id = input.Id.Value;
                }
                var entity = _aiYardDbTableIssuedCard.GetTbYardPayMonthCard(GetCurrenUserOrganizationAndChildrenUnit(), Id, "")[0];
				
                editDto = entity.MapTo<TbYardPayMonthCardEditDto>();
                 
			}
			else
			{
				editDto = new TbYardPayMonthCardEditDto();
			}

			output.TbYardPayMonthCard = editDto;
			return output;
		}


		/// <summary>
		/// 添加或者修改TbYardPayMonthCard的公共方法
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		[AbpAuthorize(TbYardPayMonthCardPermissions.Create,TbYardPayMonthCardPermissions.Edit)]
		public async Task<Dictionary<string,string>> CreateOrUpdate(CreateOrUpdateTbYardPayMonthCardInput input)
		{
			Dictionary<string, string> result = new Dictionary<string, string>();
			if (input.TbYardPayMonthCard.Station_ID <= 0)
			{
				result.Add("retype", "-1");
				result.Add("revalue", "请选择确定的场所名称!");
				return result; 
            }
			DateTime MCardBTime = (DateTime)input.TbYardPayMonthCard.MCardBTime;
			DateTime MCardETime = (DateTime)input.TbYardPayMonthCard.MCardETime;
			if (!_aiYardDbTableIssuedCard.UpdatePayMonthCard(input.TbYardPayMonthCard.CardTypeID, input.TbYardPayMonthCard.CardType, input.TbYardPayMonthCard.Station_ID, MCardBTime, MCardETime))
			{
				result.Add("retype", "-1");
				result.Add("revalue", "更新月卡时间段错误!");
				return result; 
			}
			 
			if (input.TbYardPayMonthCard.Id.HasValue)
			{
				
				await Update(input.TbYardPayMonthCard);
			}
			else
			{
				await Create(input.TbYardPayMonthCard);
			}

			result.Add("retype", "1");
			result.Add("revalue", "保存成功!");
			return result;
		}


		/// <summary>
		/// 新增TbYardPayMonthCard
		/// </summary>
		[AbpAuthorize(TbYardPayMonthCardPermissions.Create)]
		protected virtual async Task<TbYardPayMonthCardEditDto> Create(TbYardPayMonthCardEditDto input)
        {
             

            var entity=input.MapTo<TbYardPayZMonth>();
			
			entity = await _entityRepository.InsertAsync(entity);
			return entity.MapTo<TbYardPayMonthCardEditDto>();
		}

		/// <summary>
		/// 编辑TbYardPayMonthCard
		/// </summary>
		[AbpAuthorize(TbYardPayMonthCardPermissions.Edit)]
		protected virtual async Task Update(TbYardPayMonthCardEditDto input)
		{
           
            //TODO:更新前的逻辑判断，是否允许更新

            var entity = await _entityRepository.GetAsync(input.Id.Value);
			input.MapTo(entity);
              
		    await _entityRepository.UpdateAsync(entity);
		}



		/// <summary>
		/// 删除TbYardPayMonthCard信息的方法
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		[AbpAuthorize(TbYardPayMonthCardPermissions.Delete)]
		public async Task Delete(EntityDto<long> input)
		{
			//TODO:删除前的逻辑判断，是否允许删除
			await _entityRepository.DeleteAsync(input.Id);
		}



		/// <summary>
		/// 批量删除TbYardPayMonthCard的方法
		/// </summary>
		[AbpAuthorize(TbYardPayMonthCardPermissions.BatchDelete)]
		public async Task BatchDelete(List<long> input)
		{
			// TODO:批量删除前的逻辑判断，是否允许删除
			await _entityRepository.DeleteAsync(s => input.Contains(s.Id));
		}

         

        ///// <summary>
        ///// 导出TbYardPayMonthCard为excel表,等待开发。
        ///// </summary>
        ///// <returns></returns>
        //public async Task<FileDto> GetToExcel()
        //{
        //	var users = await UserManager.Users.ToListAsync();
        //	var userListDtos = ObjectMapper.Map<List<UserListDto>>(users);
        //	await FillRoleNames(userListDtos);
        //	return _userListExcelExporter.ExportToFile(userListDtos);
        //}

    }
}


